package com.furentech.iotserver.fa.sinopec.sinopec_processor;


import com.furentech.iotserver.fa.server.ChannelUtil;
import com.furentech.iotserver.fa.sinopec.SinopecFunCode;
import com.furentech.iotserver.fa.sinopec.SinopecParam;
import com.furentech.iotserver.fa.utils.SpringUtil;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

@Component
public class ReqOnlineAuth_Processor extends SinopecProcessorBase
{
    @Override
    String actualGetJsonString(byte[] buf) {
        return null;
    }

    @Override
    String actualGetReadableString(byte[] buf) {
        return "加油机在线申请授权\r\n";
    }

    @Override
    byte[] actualGetResponse(byte[] buf, Channel channel) {
        byte[] tempByteBuf = new byte[170];

        tempByteBuf[0] = SinopecFunCode.Resp_Online_Auth;
        System.arraycopy(buf, 1, tempByteBuf, 1, 5);
        tempByteBuf[6] = buf[37];

        //IdentifyChannelUtilingCode
        System.arraycopy(buf, 38, tempByteBuf, 7, 32);
        //AppID
        System.arraycopy(buf, 28, tempByteBuf, 39, 2);
        //AuthMoney
//        System.arraycopy(buf, 8, resByteBuf, 41, 3);

        SinopecParam sinopecParam = SpringUtil.getBean(SinopecParam.class);



        tempByteBuf[41]=(byte)(sinopecParam.maxAmn/65535);
        tempByteBuf[42]=(byte)(sinopecParam.maxAmn/256);
        tempByteBuf[43]=(byte)(sinopecParam.maxAmn%256);

        //PayMethod
        for (int i = 0; i < 8; i++) {
            tempByteBuf[i + 44] = 0x00;
        }

        //UUID1
        for (int i = 0; i < 32; i++) {
            tempByteBuf[i + 52] = 0x00;
        }

        //LicenseReq
        tempByteBuf[84] = 0x00;

        //LicenseNum
        for (int i = 0; i < 20; i++) {
            tempByteBuf[i + 85] = 0x20;
        }

        //FRU
        for (int i = 0; i < 10; i++) {
            tempByteBuf[i + 105] = 0x00;
        }

        //ErrorCode
        tempByteBuf[115] = 0x00;

        //ErrorCode
        for (int i = 0; i < 50; i++) {
            tempByteBuf[i + 116] = 0x20;
        }

        //MAC
        System.arraycopy(buf, 113, tempByteBuf, 166, 4);





        Byte[] tempBytes = new Byte[tempByteBuf.length];
        for (int i = 0; i < tempByteBuf.length; i++) {
            tempBytes[i]=tempByteBuf[i];
        }
        ChannelUtil.setReqOnlineRespBuf(channel, tempBytes);
        ChannelUtil.setReqOnlineFlag(channel, true);



        byte[] resByteBuf = new byte[4];

        resByteBuf[0] = SinopecFunCode.CMD_ACK;
        resByteBuf[1] = buf[0];
        resByteBuf[2] = buf[5];
        resByteBuf[3] = 0x02;

        return resByteBuf;

    }

}
